/**********************************************************
 * SendNewInvoicesAction.java.java
 * Created by Ashutosh
 * Last modified Date: Sep 18, 2008 10:49:41 AM by Ashutosh
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.AuctionInvoices;
import com.printeverywhere.dto.Transactions;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.DateUtil;

/**
 * The Class SendNewInvoicesAction.
 */
public class SendNewInvoicesAction extends AdminEngineManagement {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(SendNewInvoicesAction.class);

	private Date currentDate = DateUtil.getTodaysDate();

	private String userId;

	private Date invoiceStartDate;

	private int invoicePeriod;

	private int invoiceOverdue;

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public String sendSignleInvoice() throws Exception {
		try {
			User user = UserServiceDelegator.getUserByUserId(userId);
			if(user == null) {
				addActionError("User Id not found...");
				return INPUT;
			} else {
				List<Criterion> criterionList = new ArrayList<Criterion>();
				criterionList.add(Restrictions.eq("user", user));
				criterionList.add(Restrictions.isNull("invoice"));
				criterionList.add(Restrictions.between("date", invoiceStartDate, DateUtil.addDate(invoiceStartDate, Calendar.DATE, invoicePeriod)));
				List<Transactions> transactionList = (List<Transactions>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Transactions.class);

				AuctionInvoices auctionInvoices = new AuctionInvoices();
				auctionInvoices.setInvoice("0" + (Long.valueOf(CommonFunctionDelegator.getMaxValue(AuctionInvoices.class, "invoice").toString()) + 1));
				auctionInvoices.setUser(user);
				auctionInvoices.setDate(currentDate);
				auctionInvoices.setStart(invoiceStartDate);
				auctionInvoices.setEnds(DateUtil.addDate(invoiceStartDate, Calendar.DATE, invoicePeriod));
				auctionInvoices.setOverdue(invoiceOverdue);
				auctionInvoices.setVat(0d);
				auctionInvoices.setVatIdNr(user.getVatidnr1() + user.getVatidnr2());

				double amount = 0d;
				for (Transactions transactions : transactionList) {
					amount += transactions.getAmount();
				}

				auctionInvoices.setAmount(amount);
				CommonFunctionDelegator.generalInsertbyobj(auctionInvoices);

				for (Transactions transactions : transactionList) {
					transactions.setInvoice(auctionInvoices);
					CommonFunctionDelegator.generalUpdatebyobj(transactions);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}

		addActionMessage("Invoice has been created and send to user.");
		return SUCCESS;
	}

	public String sendMultipleInvoice() throws Exception {
		try {
			List<Criterion> criterionList = new ArrayList<Criterion>();
			criterionList.add(Restrictions.isNull("invoice"));
			criterionList.add(Restrictions.between("date", DateUtil.getDate("31-12-2007", "dd-MM-yyyy"), invoiceStartDate));
			List<User> userList = (List<User>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, Projections.groupProperty("user"), Transactions.class);

			for (User userTrans : userList) {
				criterionList.removeAll(criterionList);
				criterionList.add(Restrictions.eq("user", userTrans));
				criterionList.add(Restrictions.isNull("invoice"));
				criterionList.add(Restrictions.between("date", DateUtil.getDate("31-12-2007", "dd-MM-yyyy"), invoiceStartDate));
				List<Transactions> transactionList = (List<Transactions>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, Transactions.class);

				AuctionInvoices auctionInvoices = new AuctionInvoices();
				auctionInvoices.setInvoice("0" + (Long.valueOf(CommonFunctionDelegator.getMaxValue(AuctionInvoices.class, "invoice").toString()) + 1));
				auctionInvoices.setUser(userTrans);
				auctionInvoices.setDate(currentDate);
				auctionInvoices.setStart(invoiceStartDate);
				auctionInvoices.setEnds(DateUtil.addDate(invoiceStartDate, Calendar.DATE, invoicePeriod));
				auctionInvoices.setOverdue(invoiceOverdue);
				auctionInvoices.setVat(0d);
				auctionInvoices.setVatIdNr(userTrans.getVatidnr1() + userTrans.getVatidnr2());

				double amount = 0d;
				for (Transactions transactions : transactionList) {
					amount += transactions.getAmount();
				}

				auctionInvoices.setAmount(amount);
				CommonFunctionDelegator.generalInsertbyobj(auctionInvoices);

				for (Transactions transactions : transactionList) {
					transactions.setInvoice(auctionInvoices);
					CommonFunctionDelegator.generalUpdatebyobj(transactions);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}

		addActionMessage("Invoices has been created and send to users.");
		return SUCCESS;
	}

	public Date getCurrentDate() {
		return currentDate;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public Date getInvoiceStartDate() {
		return invoiceStartDate;
	}

	public void setInvoiceStartDate(Date invoiceStartDate) {
		this.invoiceStartDate = invoiceStartDate;
	}

	public int getInvoicePeriod() {
		return invoicePeriod;
	}

	public void setInvoicePeriod(int invoicePeriod) {
		this.invoicePeriod = invoicePeriod;
	}

	public int getInvoiceOverdue() {
		return invoiceOverdue;
	}

	public void setInvoiceOverdue(int invoiceOverdue) {
		this.invoiceOverdue = invoiceOverdue;
	}

	public static void main(String[] args) {
		SendNewInvoicesAction sendNewInvoicesAction = new SendNewInvoicesAction();
		try {
			sendNewInvoicesAction.sendMultipleInvoice();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}